package com.gzdh.dishrecognition.ui.fragment.sysset;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.dk.bleNfc.BleManager.BleManager;
import com.dk.bleNfc.BleNfcDeviceService;
import com.dk.bleNfc.DeviceManager.BleNfcDevice;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.config.SysBluetoothConfig;
import com.gzdh.dishrecognition.event.ScanBlueEvent;
import com.gzdh.dishrecognition.model.BluetoothModel;
import com.gzdh.dishrecognition.ui.adapter.SysBluetoothAdapter;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SysBluetoothFragment extends Fragment {
    private Context mContext;
    private Activity mActivity;

    SysBluetoothAdapter sysBluetoothAdapter;
    BleNfcDeviceService bleNfcDeviceService;

    public void setBleNfcDeviceService(BleNfcDeviceService bleNfcDeviceService) {
        this.bleNfcDeviceService = bleNfcDeviceService;
    }

    private Lock mNearestBleLock = new ReentrantLock();// 锁对象

    public SysBluetoothFragment(Activity activity) {
        this.mActivity = activity;

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = inflater.getContext();
        View view = getViews();

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        BleNfcDevice bleNfcDevice = bleNfcDeviceService.bleNfcDevice;
        if ((bleNfcDevice.isConnection() == BleManager.STATE_CONNECTED)) {
            BluetoothModel bluetoothModel = new BluetoothModel();
            bluetoothModel.setName(bleNfcDevice.getDeviceName());
            bluetoothModel.setAddress("已链接");
            bluetoothModel.setSelect(1);
            sysBluetoothAdapter.addBluetooth(bluetoothModel);
        }
    }


    private View getViews() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        LinearLayout mainLayout = new LinearLayout(mContext);
        mainLayout.setLayoutParams(layoutParams);
        mainLayout.setBackgroundColor(Color.WHITE);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        mainLayout.addView(getTitleView());   //标题初始化
        mainLayout.addView(getBody());        //主题内容
        return mainLayout;
    }


    @SuppressLint("ResourceType")
    private View getBody() {
        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

        RecyclerView recyclerView = new RecyclerView(mContext);
        recyclerView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        sysBluetoothAdapter = new SysBluetoothAdapter(mContext);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(sysBluetoothAdapter);
        linearLayout.addView(recyclerView);
        sysBluetoothAdapter.setOnUserClick(bluetoothModel -> {

            SysBluetoothConfig.getInstance().setAddress(bluetoothModel.getAddress());
            bleNfcDeviceService.reConnect();
            sysBluetoothAdapter.clear();
        });

        return linearLayout;
    }

    private View getTitleView() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout titleLinear = new LinearLayout(mContext);
        titleLinear.setLayoutParams(layoutParams);
        titleLinear.setBackgroundColor(Color.WHITE);
        titleLinear.setOrientation(LinearLayout.VERTICAL);

        RelativeLayout titleRv = new RelativeLayout(mContext);
        titleRv.setLayoutParams(layoutParams);
        titleLinear.addView(titleRv);

        //-------------------------标题
        TextView titleTv = new TextView(mContext);
        titleTv.setText(" 蓝  牙  NFC 设 置 ");
        RelativeLayout.LayoutParams tt1 = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        int tt1Margins = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, this.getResources().getDisplayMetrics());
        tt1.setMargins(tt1Margins, tt1Margins, tt1Margins, tt1Margins);
        tt1.addRule(RelativeLayout.CENTER_IN_PARENT);
        titleTv.setLayoutParams(tt1);
        titleTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        titleRv.addView(titleTv);


        TextView scanBut = new TextView(mContext);
        tt1 = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        tt1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        tt1.setMargins(tt1Margins, 5, tt1Margins, 5);
        tt1.addRule(RelativeLayout.CENTER_VERTICAL);
        scanBut.setLayoutParams(tt1);
        scanBut.setText("搜索");
        scanBut.setTextColor(Color.WHITE);
        scanBut.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        scanBut.setPadding(20, 10, 20, 10);
        scanBut.setBackground(getResources().getDrawable(R.drawable.bg_btn_pay));
        titleRv.addView(scanBut);


        scanBut.setOnClickListener(view -> {
            if (bleNfcDeviceService != null) {
                bleNfcDeviceService.setType(1);
                bleNfcDeviceService.startScan();
            }


        });
        //----------------------------下划线
        View view = new View(mContext);
        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        view.setBackgroundColor(Color.rgb(224, 224, 224));
        titleLinear.addView(view);

        return titleLinear;
    }


    public void addBlue(ScanBlueEvent scanBlueEvent) {
        BluetoothModel bluetoothModel = new BluetoothModel();
        bluetoothModel.setName(scanBlueEvent.getName());
        bluetoothModel.setAddress(scanBlueEvent.getAddress());
        sysBluetoothAdapter.addBluetooth(bluetoothModel);
    }

}
